<?php

namespace app\common\controller;

use app\admin\controller\system\AdminUser;
use app\common\validate\AdminValidate;
use app\Query;
use Symfony\Component\Translation\Dumper\CsvFileDumper;
use think\Exception;
use think\validate\ValidateRule;

trait ResourceController
{

    /**
     * @var Query $model
     */
    public $model;

    public function index()
    {
        $params = request()->only($this->model->getTableFields());

        $query = $this->model;

        $res = $query
            ->when(property_exists($this, 'defaultWhere') && $this->defaultWhere, function ($query) {
                $query->where($this->defaultWhere);
            })
            ->when(property_exists($this, 'with') && $this->with, function ($query) {
                $query->with($this->with);
            })
            ->when($params, function ($query) use ($params) {
                $query->where($params);
            })
            ->order('updated_at', 'desc')->paginate();

        return success('', $res);
    }

    public function save()
    {

        $params = input();

        if (property_exists($this->model, 'validate')) {

            /**
             * @var AdminValidate $validate
             */
            $validate = new $this->model->validate;

            if (!empty($params['id'])) {
                if ($validate->hasScene('edit')) {
                    $validate->scene('edit');
                };
            } else {
                if ($validate->hasScene('add')) {
                    $validate->scene('add');
                };
            }


            if (!$validate->check($params)) {
                return fail($validate->getError());
            }
        }


        if (empty($params['id'])) {
            $newClass = get_class($this->model);
            $obj = new $newClass($params);
            $obj->save();
            return success();
        }

        //不能改的字段
        if (property_exists($this, 'noEditField')) {
            foreach ($this->noEditField as $value) {
                unset($params[$value]);
            }
        }
        $obj = $this->model->where('id', $params['id'])->find();

        if (!$obj) {
            return fail('数据不存在');
        }

        $obj->strict(false)->save($params);

        return success();

    }

    public function delete()
    {

        if (empty(input('id'))) {
            return fail();
        }


        try {

            if (property_exists($this, 'deleteNotAllowed') && $this->deleteNotAllowed) {
                throw new Exception('不允许删除');
            }

            $obj = $this->model->where('id', intval(input('id')))->find();

            if (!$obj) {
                throw new Exception('不存在数据');
            }

            if (method_exists($this, 'deleteBefore')) {
                $this->deleteBefore($obj);
            }

            if (property_exists($this, 'deleteField') && $this->deleteField) {
                $obj->save($this->deleteField);
            } else {
                $obj->delete();
            }


            return success();

        } catch (\Exception $exception) {

            return fail($exception->getMessage());

        }


    }
}